﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using _4chanManager.Logic;
using _4chanManager.Properties;

namespace _4chanManager.Panels
{
    /// <summary>
    /// Interaction logic for WaitForThreadPanel.xaml
    /// </summary>
    public partial class WaitForThreadPanel : UserControl
    {

        private BackgroundWorker _bgw;
        private List<string> keyList;
        private int SliderValue = 1;
        private bool containAll = false;

        public WaitForThreadPanel()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            _bgw = new BackgroundWorker { WorkerReportsProgress = true };
            _bgw.DoWork += bgw_DoWork;

            txtKeywords.Text = (string) Settings.Default["strKeywords"];

        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            KeywordMonitor kw = new KeywordMonitor(keyList, pathWebAdress.Path)
            {
                minutes = SliderValue,
                containAll = containAll
            };
            kw.ThreadExists += kw_ThreadExists;
            kw.Start();
        }

        void kw_ThreadExists()
        {
            MessageBox.Show("THREAD HAS JUST ARRIVED", "Thread Monitor", MessageBoxButton.OK,
                MessageBoxImage.Information);
        }

        

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            keyList = txtKeywords.Text.Split(';').Select(a => a.ToLower().Trim()).ToList();

            _bgw.RunWorkerAsync();
        }

        private void btnSaveKeywords_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default["strKeywords"] = txtKeywords.Text;
            Settings.Default.Save();
        }

        private void chkAllKeywords_Click(object sender, RoutedEventArgs e)
        {
            containAll = (bool)chkAllKeywords.IsChecked;
        }

        private void sliderRefresh_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if(lblMinutes != null)
                lblMinutes.Content = e.NewValue + " min";
            SliderValue = Int32.Parse(e.NewValue.ToString(CultureInfo.InvariantCulture));
        }

       
    }
}
